package Semester_3.FilterChecking;

import Semester_3.ShoppingCart.cookie;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter("/FilterChecking/index.jsp")
public class intercept implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        Cookie[] cookies = req.getCookies();
        for(Cookie c : cookies){
            String value = c.getName();
            if(value.equals("mark")){
                filterChain.doFilter(servletRequest,servletResponse);
            }else {
                req.setAttribute("Filter_index",1);
                req.getRequestDispatcher("/FilterChecking/login.jsp").forward(req,resp);
            }
        }

    }

    @Override
    public void destroy() {

    }
}
